package com.aoip.telemetry.telemetry.configureui.business.order.send.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aoip.telemetry.telemetry.configureui.business.order.send.service.OrderRecordService;
import com.aoip.telemetry.telemetry.configureui.business.order.send.service.OrderSendService;
import com.aoip.telemetry.telemetry.configureui.common.core.controller.BaseController;
import com.aoip.telemetry.telemetry.configureui.common.core.page.TableDataInfo;
import com.aoip.telemetry.telemetry.configureui.common.utils.http.HttpUtils;
import com.aoip.telemetry.telemetry.core.constant.HttpStatus;
import com.aoip.telemetry.telemetry.core.entity.AjaxResult;
import com.aoip.telemetry.telemetry.entity.equipment.QuerySetOrderEntity;
import com.aoip.telemetry.telemetry.entity.equipment.StOnlineStatusEntity;
import com.aoip.telemetry.telemetry.entity.order.OrderRecordEntity;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author chenbai
 * @version 1.0
 * @date 2022/2/23 14:30
 */
@RestController
@ConfigurationProperties(prefix = "receive.url")
public class OrderSendController extends BaseController {

    @Resource
    private OrderSendService service;
    @Resource
    private OrderRecordService recordService;

    @Value("${receive.url.address}")
    private String address;

    /**
     * 获取设备在线状态分页列表
     *
     * @param entity 测站
     * @return com.aoip.telemetry.telemetry.configureui.original.core.list.TableDataInfo
     * @author chenbai
     * @date 2022/2/22
     **/
    @GetMapping("/v1/GET/order/send/page")
    public TableDataInfo page(StOnlineStatusEntity entity) {
        startPage();
        List<StOnlineStatusEntity> list = service.list(entity);
        return getDataTable(list);
    }

    @PostMapping("/v1/POST/order/send/generate")
    public AjaxResult generateOrder(@Validated @RequestBody JSONObject params) {
        List<String> msgList = JSON.parseArray(JSON.toJSONString(params.get("msg")), String.class);
        String orderId = JSON.parseObject(JSON.toJSONString(params.get("orderId")), String.class);
        String stCode = JSON.parseObject(JSON.toJSONString(params.get("stCode")), String.class);
        QuerySetOrderEntity order = service.generateOrder(msgList, orderId, stCode);
        if (order == null) {
            return AjaxResult.error("指令未配置或指令错误！");
        }
        String s = HttpUtils.sendPost(address + "/v1/POST/order/call", order);
        AjaxResult result = JSONObject.parseObject(s, AjaxResult.class);
        if (HttpStatus.SUCCESS == (int)result.get(AjaxResult.CODE_TAG)) {
            OrderRecordEntity entity = new OrderRecordEntity();
            entity.setOrderId(orderId);
            entity.setStCode(stCode);
            entity.setOrderNum(0);
//            entity.setStatus();
            recordService.insert(entity);
        }
        return result;
    }

}
